package com.example.owner.lettery1.adapter

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.owner.lettery1.R
import com.example.owner.lettery1.bean.Record
import com.example.owner.lettery1.modlus.OnItemListener
import com.example.owner.lettery1.modlus.SplitBall


class RecordAdapter(private var list: ArrayList<Record>) : RecyclerView.Adapter<RecordAdapter.LotteryHolder>() {
    var onItemListener: OnItemListener<Record>? = null


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LotteryHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_lottory, parent, false)
        val holder = LotteryHolder(itemView)
        holder.onItemListener = onItemListener
        return holder
    }

    override fun getItemCount(): Int = list.size

    override fun onBindViewHolder(holder: LotteryHolder, position: Int) {
        holder.bindData(list[position])
    }


    inner class LotteryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var onItemListener: OnItemListener<Record>? = null
        private var lotteryNameView: TextView? = null
        private var right_icon: ImageView? = null
        private var issueNameView: TextView? = null
        private var dateView: TextView? = null
        private var recycleView: RecyclerView? = null

        init {
            lotteryNameView = itemView.findViewById(R.id.lotteryNameView)
            right_icon = itemView.findViewById(R.id.right_icon)
            issueNameView = itemView.findViewById(R.id.issueNameView)
            dateView = itemView.findViewById(R.id.dateView)
            recycleView = itemView.findViewById(R.id.recycleView)
        }


        fun bindData(bean: Record) {
            right_icon!!.visibility = View.GONE
            lotteryNameView!!.text = bean.lotteryName
            val issue = "第" + bean.issue + "期"
            issueNameView!!.text = issue
            dateView!!.text = bean.time!!.substring(0, 10)
            val ballList = SplitBall.splitBall(bean.awardNum!!)
            val llm = LinearLayoutManager(itemView.context)
            llm.orientation = LinearLayoutManager.HORIZONTAL
            recycleView!!.layoutManager = llm
            recycleView!!.adapter = BallAdapter(ballList, bean.lotteryId!!)
            recycleView!!.isNestedScrollingEnabled = false
            itemView.setOnClickListener {
                if (onItemListener != null)
                    onItemListener!!.onClick(bean)
            }
        }
    }
}